//数据管理 服务器 数据 内存数据 等等




export default class DataMgr {
    private static instance: DataMgr;
    private gameData = {};  //游戏数据

    public subGameInfo: any = null;

    public token: any = null;
    public uid: any = null;
    public userInfo: any = {
        coin: 0,
        list: [],
        serverGet: 0,
        redPack: 0,
        superior: '',
        rolling: 0
    }

    public rollingPool: number = 0;
    public static Ins() {
        if (!this.instance) {
            this.instance = new DataMgr();
            this.instance.init()
        }
        return this.instance;
    }

    init() {

        this.userInfo = {
            coin: 0,
            list: [],
            serverGet: 0,
            redPack: 0,
            superior: '',
            rolling: 0
        }

        this.subGameInfo = [
            {
                "name": "gameBlock",
                "sceneName": "gameBlock",
                "rankType": "block",
                'gameName': '方块消除术'
            },
            // {
            //     "name": "gameSnake",
            //     "sceneName": "gameSnake",
            //     "rankType": "snake",
            //     'gameName': '蛇与方块'

            // },
            {
                "name": "gameMoreMoney",
                "sceneName": "gameMoreMoney",
                "rankType": "moreMoney",
                'gameName': '赚他一个亿'
            },
            {
                "name": "gameEat",
                "sceneName": "gameEat",
                "rankType": "eat",
                'gameName': '吃不到我啊'
            },
            {
                "name": "gameFire",
                "sceneName": "gameFire",
                "rankType": "gameFire",
                'gameName': '海岛奇兵'
            }
        ]
    }

    setLocalStorage(key: string, value: any) {
        cc.sys.localStorage.setItem(key, JSON.stringify({ value }));
    }

    getLocalStorage(key: string) {
        let item = cc.sys.localStorage.getItem(key);
        try {
            item = JSON.parse(item);
            return item.value;
        } catch (_) {
            return;
        }
    }

    removeLocalStorage(key: string) {
        cc.sys.localStorage.removeItem(key);
    }

    clearLocalStorage() {
        cc.sys.localStorage.clear();
    }


    setGameData(key: string, value: any) {
        this.gameData[key] = value;
    }

    getGameData(key: string) {
        return this.gameData[key];
    }

    deleteGameData(key: string | null = null) {
        if (key) {
            delete this.gameData[key];
        } else {
            this.gameData = {};
        }
    }

    formatData(dayAdd: number = 0, moreAdd: boolean = false) {
        const today = new Date(Date.now() + dayAdd * 24 * 60 * 60 * 1000);
        const year = today.getFullYear();
        const month = String(today.getMonth() + 1).padStart(2, '0');
        const day = String(today.getDate()).padStart(2, '0');
        const formattedDate = `${year}-${month}-${day}`;


        if (moreAdd) {
            const h = today.getHours();
            const m = today.getMinutes();

            return `${year}-${month}-${day} ${h}:${m}`;
        }

        // console.log(formattedDate);
        return formattedDate;
    }



    formateDataByTime(time, moreAdd: boolean = false) {
        const today = new Date(time);
        const year = today.getFullYear();
        const month = String(today.getMonth() + 1).padStart(2, '0');
        const day = String(today.getDate()).padStart(2, '0');
        const formattedDate = `${year}-${month}-${day}`;


        if (moreAdd) {
            const h = today.getHours();
            const m = today.getMinutes();

            return `${year}-${month}-${day} ${h}:${m}`;
        }

        // console.log(formattedDate);
        return formattedDate;
    }


    getLeftVideoTimes() {
        const time = this.getLocalStorage('leftVideoTimes');
        console.log('getLeftVideoTimes = ', time)
        if (time) {
            const data = this.formatData();
            const times = time.split('_');
            if (times[0] == data) {
                return Number(times[1]);
            } else {
                return 7;
            }

        } else {
            return 7;
        }
    }


    setLeftVideoTimes(times: number) {
        const data = this.formatData();
        this.setLocalStorage('leftVideoTimes', `${data}_${times}`);
    }




    refreshUserInfo(info) {
        console.log('refresh User = ', info);

        this.userInfo.list = info.list.slice(0, 50);
        // this.userInfo.serverGet = info.serverGet;
        // this.userInfo.redPack = Number(info.coin / 10);
    }


}